jQuery(function() {
	var doc = document,
		$doc = $(doc);

	//切换右侧模块
	!function getULElement() {
		var lis = $(".sidebar li"),
			chunks = $(".chunks"),
			fNavLi = $(".f_nav li"),
			addExample = $(".addExample .add");

		/*我是服务商列表切换 start*/ 
			var count = {
				num: 3
			},
			example = {
				title: false,
				desc: false,
				link: false
			},
			facilitator = {
				type: 0,
				name: false,
				desc: false,
				trade: false,
				peoples: false,
				plan: false,
				service: false
			},
			base = { //基本信息
				nickName: false,
				realName: false,
				// areas: false,
				contact: false,
				qq: false,
				email: false
			};
			//切换小导航
		function showMinMenu(self, elm, nav) {
			var index = self.index();

			$.each(nav, function(i, item) {
				item.className = "";
				elm[i].style.display = "none";
			});
			elm[index].style.display = "block";
			nav[index].className = "selected";
		}
			//显示是否上传案例
		function showExampleTable(elm) {
			elm.toggle("show")
		}
		//生成案例
		function createExampple(eid,imgurl,title, desc, link) {

			$(".example .content").prepend(
				'<div data-index=' + eid + '>' +
					'<dl>' +
						'<dt><a href="#"><img src="'+imgurl+'"></a></dt>' +
						'<dd>' +
							'<p><span class="btnBox"><button class="remove" data-index=' + eid + '>删除</button><button class="edit" data-index=' + eid + '>编辑</button></span><a href="#">[' + title + ']</a></p>' +
							'<p class="desc">' + desc + '</p>' +
							'<a href="#">' + link + '</a>' +
						"</dd>" +
					"</dl>"+
				"</div>"
			);
		}

		//清除错误信息
		function clearErrMessage(input, node) {
			$doc.on("focus", input, function() {

				utils.clearErrMessage(node);
			});
		}
		//设置删除按钮的index
		!function setIndex() {
			var elm = $(".example .remove"),
				len = elm.length;

			for (var i = 0; i < len; i++) {
				elm[i].index = i;
			}
		}();

		lis.on("click", function() {
			var self = $(this);

			$.each(lis,function(i, item) {
				item.className = "b_" + (i + 1);
				chunks[i].style.display = "none";
			});

			self.addClass("selected");
			chunks[self.index()].style.display = "block";
		});

			//团队案例列切换 start
		fNavLi.on("click", function() {
			showMinMenu($(this), $(".facilitator > div"),fNavLi);
		});
			//显示案例表单
		$doc.on("click", ".edit", function() {
			var index = $(this).attr("data-index");
			$.ajax({
				type:"post",
				url:getExamInfoUrl,
				dataType:"json",
				data:{'id':index},
				success:function(obj){
					if(obj.level == 'success'){
						var d = new Date();
						var imgurl = imgUrl+ '/'+ obj.data.img_exam_id + '?' + d.getTime();
						$(".exampleTitle input").val(obj.data.title);
						$(".exampleDesc textarea").val(obj.data.org_desc);
						$(".exampleLink input").val(obj.data.url);
						$("#examImgId").val(obj.data.img_exam_id);
						$("#target").attr('src',imgurl);
						$('.save').attr('data-index',obj.data.id);
						showExampleTable($(".uploadExample"));
					}
				},
				error:function(obj){
					var datajson = obj.responseJSON;
					noty({text:datajson.message,type:datajson.level,timeout:1000});
				}
			});
		});

		// $(".example .save").on("click", function() {
		// 	showExampleTable($(".uploadExample"));
		// });

		$(".example .clear").on("click", function() {
			showExampleTable($(".uploadExample"));
			$(".exampleTitle input").val("");
			$(".exampleDesc textarea").val("");
			$(".exampleLink input").val("");
			$("#examImgId").val("");
			$(".save").attr("data-index",'0');
			$("#target").attr('src',modelImg);
		});

		addExample.on("click", function() {
			showExampleTable($(".uploadExample"));
		});
			//团队案例列切换 end
			//生成案例
		$doc.on("click", ".uploadExample .save", function() {
			var title = $(".exampleTitle input").val(),
				desc = $(".exampleDesc textarea").val(),
				link = $(".exampleLink input").val();

			if (!utils.empty(title)) {
				example.title = true;
			} else {
				example.title = false;
				utils.errorMessage($(".titleError"), "*必填");
			}

			if (!utils.empty(desc)) {
				example.desc = true;
			} else {
				example.desc = false;
				utils.errorMessage($(".descError"), "*必填");
			}

			if (!utils.empty(link)) {
				example.link = true;
			} else {
				example.link = false;
				utils.errorMessage($(".linkError"), "*必填");
			}

			if (example.title && 
				example.desc &&
				example.link) {
				var index = $(this).attr("data-index");
				setExamInfo(index);
			}
		});
		//提交案例信息
		function setExamInfo(index){
			$.ajax({
				type:"post",
				url:setExamInfoUrl + '/' + index,
				dataType:"json",
				data:$('#setExamInfo').serialize(),
				success:function(obj){
					if(obj.level == 'success'){
						noty({text:'提交成功',type:obj.level,timeout:500,
							callback:{
								afterClose:function(){
									$(".exampleTitle input").val("");
									$(".exampleDesc textarea").val("");
									$(".exampleLink input").val("");
									$("#target").attr('src',modelImg);
									var d = new Date();
									var imgurl = imgUrl+ '/'+ obj.data.img_exam_id + '?' + d.getTime();
									if(index == 0){
										createExampple(obj.data.id,imgurl,obj.data.title, obj.data.org_desc, obj.data.url);
									}else{
										$('#e'+ index+' #exam_img').attr('src',imgurl);
										$('#e'+ index+' #exam_title').html(obj.data.title);
										$('#e'+ index+' #exam_desc').html(obj.data.org_desc);
										$('#e'+ index+' #exam_url').html(obj.data.url);
									}
									showExampleTable($(".uploadExample"));
								}
							}
						});
					}
				},
				error:function(obj){
					var datajson = obj.responseJSON;
					noty({text:datajson.message,type:datajson.level,timeout:1000});
				}
			});
		}
			//清除错误信息
		clearErrMessage(".exampleTitle input",$(".titleError"));
		clearErrMessage(".exampleDesc textarea", $(".descError"));
		clearErrMessage(".exampleLink input", $(".linkError"));

			//删除案例
		$doc.on("click", ".example .remove",function() {
			var index = $(this).attr("data-index"),
				elm = $(".example .content > div"),
				parent = $("[data-index= " + index + "]");

			$.ajax({
				type:"post",
				url:delExamInfo,
				dataType:"json",
				data:{'id':index},
				success:function(obj){
					if(obj.level == 'success'){
						parent.fadeOut();
						setTimeout(function() {
							parent.remove();
						}, 500);
					}
				},
				error:function(obj){
					var datajson = obj.responseJSON;
					noty({text:datajson.message,type:datajson.level,timeout:1000});
				}
			});
		});

			/*团队管理 start*/
			//隐藏公司名和简介
		$doc.on("click", "[name=type]", function() {
			var personDesc = $(".personDesc"),
				type = $(this)[0].getAttribute("data-type"),
				companyName = $(".companyName > em"),
				companyDesc = $(".companyDesc > em");

			if ($(this).hasClass("personal")) {
				$(".team .comp").hide();
				personDesc.show();
			} else {
				$(".team .comp").show();
				personDesc.hide();
			}

			if (type === "1") {
				companyName.html("工作室名字");
				companyDesc.html("工作室简介");
			}

			if (type === "2") {
				companyName.html("公司名字");
				companyDesc.html("公司简介");
			}
		});
			//提交表单
		$doc.on("click", ".facilitator .team button", function() {
			var companyName = $(".companyName input").val(),
				companyDesc = $(".companyDesc textarea").val(),
				personDesc = $(".personDesc input").val(),
				exper = $(".exper input").val(),
				peoples = $(".peoples input").val(),
				plan = $(".plan textarea").val(),
				service = $(".service input").val();

			//是否选中个人
			if ($("[data-type=0]").prop("checked")) {
				facilitator.type = 0; 
				facilitator.name = true; //当个人是则不显示
				facilitator.desc = true; //当个人是则不显示
				facilitator.peoples = true; //当个人是则不显示
			} else {

				if (!utils.empty(companyName)) {
					facilitator.name = true;
				} else {
					facilitator.name = false;
					utils.errorMessage($(".compn_error"), "*必填");
				}

				if (!utils.empty(companyDesc)) {
					facilitator.desc = true;
				} else {
					facilitator.desc = false;
					utils.errorMessage($(".compd_error"), "*必填");
				}

				if (!utils.empty(peoples)) {
					facilitator.peoples = true;
				} else {
					facilitator.peoples = false;
					utils.errorMessage($(".peoples_error"), "*必填");
				}
			}

			if (!utils.empty(plan)) {
				facilitator.plan = true;
			} else {
				facilitator.plan = false;
				utils.errorMessage($(".plan_error"), "*必填");
			}

			if (!utils.empty(service)) {
				facilitator.service = true;
			} else {
				facilitator.service = false;
				utils.errorMessage($(".service_error"), "*必填");
			}

			if (facilitator.name &&
				facilitator.desc &&
				facilitator.peoples &&
				facilitator.plan &&
				facilitator.service) {
				//如果选择个人的情况下不提交 '公司名字 公司简介 公司人数'
				//do something
				var province = $('#o_province option:selected') .val();
				var city = $('#o_city option:selected') .val();
				var county = $('#o_county option:selected') .val();
				if(province !== '省份' && city !== '地级市' && county !== '县级市'){
					setOrgInfo();
				}else{
					noty({text:'请将【所在地区】选择完整',type:'error',timeout:500});
				}

			}
		});

		function setOrgInfo(){
			var type = $('input[name="type"]:checked').val();
			var org_name = $('input[name="org_name"]').val();
			var org_desc = $('textarea[name="org_desc"]').html();
			var people_desc = $('textarea[name="people_desc"]').html();
			var province = $('#o_province').val();
			var city = $('#o_city').val();
			var county = $('#o_county').val();
			var experience = $('.exper').val();
			var fields = [];
			$('input[name="field"]:checked').each(function () {
				fields.push($(this).val());
			});
			var skills = [];
			$('input[name="skill"]:checked').each(function () {
				skills.push($(this).val());
			});

			var has_invoice = $('select[name="has_invoice"]').val();
			var team_num = $('input[name="team_num"]').val();
			var solution = $('textarea[name="solution"]').html();
			var other_service = $('input[name="other_service"]').val();
			var state = 1;
			$.ajax({
				type:"post",
				url:setOrgInfoUrl,
				dataType:"json",
				data:{'type':type,'people_desc':people_desc,'org_name':org_name,'org_desc':org_desc,
					'o_province':province,'o_city':city,'o_county':county,'field':fields,'skill':skills,
					'has_invoice':has_invoice,'experience':experience,'team_num':team_num,'solution':solution,'other_service':other_service,'state':state},
				success:function(obj){
					if(obj.level == 'success'){
						noty({text:"保存成功",type:obj.level,timeout:800});
					}
				},
				error:function(obj){
					var datajson = obj.responseJSON;
					noty({text:datajson.message,type:datajson.level,timeout:1000});
				}
			});
		}
		//清除错误信息
		clearErrMessage(".companyName input", $(".compn_error"));
		clearErrMessage(".companyDesc textarea", $(".compd_error"));
		clearErrMessage(".trade input", $(".trade_error"));
		clearErrMessage(".peoples input", $(".peoples_error"));
		clearErrMessage(".plan textarea", $(".plan_error"));
		clearErrMessage(".service input", $(".service_error"));
		/*我是服务商列表切换 end*/


		/*我是推广员 start*/
		$doc.on("click", ".inviteCode > ul > li", function() {
			showMinMenu($(this), $(".inviteCode > div"), $(".inviteCode > ul > li"));
		});
		/*我是推广员 end*/

		/*基本信息 start*/
		$doc.on("click", ".makeOut .save",function() {
			var nickName = $(".module .nickName input").val(),
				realName = $(".module .realName input").val(),
				contact = $(".module .contact input").val(),
				qq = $(".module .qq input").val(),
				email = $(".module .email input").val();

			if (!utils.empty(nickName)) {
				base.nickName = true;
			} else {
				base.nickName = false;
				utils.errorMessage($(".nickName_error"), "*必填");
			}

			if (!utils.empty(realName)) {
				base.realName = true;
			} else {
				base.realName = false;
				utils.errorMessage($(".realName_error"), "*必填");
			}

			if (!utils.empty(contact)) {
				base.contact = true;
			} else {
				base.contact = false;
				utils.errorMessage($(".contact_error"), "*必填");
			}

			if (!utils.empty(qq)) {
				base.qq = true;
			} else {
				base.qq = false;
				utils.errorMessage($(".qq_error"), "*必填");
			}

			if (!utils.empty(email)) {
				base.email = true;
			} else {
				base.email = false;
				utils.errorMessage($(".email_error"), "*必填");
			}

			if (base.nickName &&
				base.realName &&
				base.contact &&
				base.qq &&
				base.email) {
				var province = $('#s_province option:selected') .val();
				var city = $('#s_city option:selected') .val();
				var county = $('#s_county option:selected') .val();
				if(province !== '省份' && city !== '地级市' && county !== '县级市'){
					setFacInfo();
				}else{
					noty({text:'请将【所在地区】选择完整',type:'error',timeout:500});
				}
			}
		});

		function setFacInfo(){
			$.ajax({
				type:"post",
				url:setFacInfoUrl,
				dataType:"json",
				data:$('#user_info').serialize(),
				success:function(obj){
					if(obj.level == 'success'){
						noty({text:"保存成功",type:obj.level,timeout:800});
					}
				},
				error:function(obj){
					var datajson = obj.responseJSON;
					noty({text:datajson.message,type:datajson.level,timeout:1000});
				}
			});
		}
		$doc.on("click", "#goPop", function() {
			window.location.href=checkPop;
		});
			//清除【基本信息】错误信息
		clearErrMessage(".nickName input", $(".nickName_error"));
		clearErrMessage(".realName input", $(".realName_error"));
		clearErrMessage(".contact input", $(".contact_error"));
		clearErrMessage(".qq input", $(".qq_error"));
		clearErrMessage(".email input", $(".email_error"));
		/*基本信息 end*/
	}();
});